-- Copyright (C) 2013 wgm.china@gmail.com (������)
--vss.db.methodΪ����list�����Ĳ�������
--date:2013-12-11

local strfind = string.find
local strsub  = string.sub
local strbyte = string.byte
local strrep  = string.rep
local strchar = string.char
local strrep  = string.rep
local strlen  = string.len
local type    = type
local pairs   = pairs
local tonumber= tonumber
local table   = table

local tb_insert = table.insert
local tb_concat = table.concat
local tb_remove = table.remove

-------1���幫�����(��ʼ)---------------
local data_util           = require("gcs.mongodb.utils")
local utils           = require("gcs.utils")

-------1���幫�����������----------------

module(...)
local version = '1.1'

-------2���������ļ�-----------------------

-------2���������ļ�������----------------
--return version �б���İ汾��
function get_version()
  return version
end

function save_data(table_name,db_data)
  --�������Ϊtable
  if type(db_data) ~= "table" then
    return nil,0,"Parameter must be table format"
  end
  if type(table_name) ~="string" then
    return nil,0,"table_name must be string format"
  end
  local conn,db,err= data_util.get_data_db()
  if not conn then
    return nil,"mongodb connect err"..err
  end
  local col,err = db:get_col(table_name)  --取col对象
  if not col then
    return nil,"get col is err:"..err
  end
  local insert_data = {}
  insert_data[1] = db_data
  local n,err = col:insert(insert_data,nil,true)    --插入完成
  data_util.close_data_db(conn)
  return n,err
end



--update_data格式 {C_KEY=1,C_VALUE=1}  修改字段
--selector格式{C_TYPE='hs'} 查询条件
function update_data(table_name,update_data,selector)
  --�������Ϊtable
  if type(update_data) ~= "table" then
    return nil,"Parameter must be table format"
  end
  if type(selector) ~= "table" then
    return nil,"Parameter must be table format"
  end
  if type(table_name) ~="string" then
    return nil,"table_name must be string format"
  end
  utils.gcs_log("error","con time1")
  local conn,db,err= data_util.get_data_db()
  if not conn then
    return nil,"mongodb connect err"..err
  end
  utils.gcs_log("error","con time2")
  utils.gcs_log("error","col time1")
  local col,err = db:get_col(table_name)  --取col对象
  if not col then
    return nil,"get col is err:"..err
  end
  utils.gcs_log("error","col time2")
  local update = {}
  update["$set"] = update_data
  utils.gcs_log("error","update time1")
  local n,err = col:update(selector,update,nil,nil,true) --执行操作
  utils.gcs_log("error","update time2")
  data_util.close_data_db(conn)
  return n,err
end




--selector格式{C_TYPE='hs'} 查询条件
function del_data(table_name,selector)
  if type(table_name) ~= "string" then
    return nil,"Parameter must be string format"
  end
  if type(selector) ~="table" then
    return nil,"table_name must be string format"
  end
  local conn,db,err= data_util.get_data_db()
  if not conn then
    return nil,"mongodb connect err"..err
  end
  local col,err = db:get_col(table_name)  --取col对象
  if not col then
    return nil,"get col is err:"..err
  end
  local n,err = col:delete(selector,nil,true)   --执行delete
  data_util.close_data_db(conn)
  return n,err
end


--query格式 {C_TYPE='hs'} 查询条件
--returnfields格式{n=1,j=1} 要查字段
function select_data(table_name,query,returnfields)
  if type(table_name) ~= "string" then
    return nil,nil,"Parameter must be string format"
  end
  if returnfields then
    if type(returnfields) ~= "table" then
      return nil,nil,"Parameter must be string format"
    end
  end
  if type(query) ~= "table" then
    return nil,nil,"Parameter must be string format"
  end
  local conn,db,err= data_util.get_data_db()
  if not conn then
    return nil,nil,"mongodb connect err"..err
  end
  local col,err = db:get_col(table_name)  --取col对象
  if not col then
    return nil,nil,"get col is err:"..err
  end
  local cursor,err = col:find(query,returnfields,0) --执行select
  return conn,cursor,err
end

function count_data(table_name,query)
  local conn,db,err= data_util.get_data_db()
  if not conn then
      return nil,nil,"mongodb connect err"..err
  end
  local col,err = db:get_col(table_name)  --取col对象
  if not col then
      return nil,nil,"get col is err:"..err
  end
  local count,err=col:count(query)
  return count
end


function close_db(conn)
  return data_util.close_data_db(conn)
end